﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using WX.WMS.DataModel;
using WX.WMS.Common;
using WX.WMS.BLL;

public partial class ClientManager : PageBase
{
    private string action
    {
        get
        {
            if (ViewState["action"] == null) return "";
            else return ViewState["action"].ToString();
        }
        set { ViewState["action"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            action = Request.QueryString["action"].ToString();
            if (action == "add")
            {
                lblTitle.Text = "添加客户";                
            }
            else
            {
                lblTitle.Text = "编辑客户";

                InitData();
            }
            
        }
        
     }

    protected void InitData()
    {
        ClientBLL BllClient = new ClientBLL();
        Name.ReadOnly = true;
        string ID = Request.QueryString["ID"].ToString();
        T_CLIENT Client = BllClient.GetClientByID(ID);
        Name.Text = Client.NAME;
        Contact.Text=Client.CONTACT;
        Tel.Text=Client.TEL;
        Address.Text=Client.ADDRESS;
    }

    protected void Add_Submit_Click(object sender, EventArgs e)
    {
        ClientBLL BllClient = new ClientBLL();
        if (action == "add")
        {
            T_CLIENT Client = BllClient.GetClientByName(Name.Text.Trim());
            if (Client == null)
            {
                Client = new T_CLIENT();
                Client.ADDRESS = Address.Text.Trim();
                Client.CONTACT = Contact.Text.Trim();
                Client.NAME = Name.Text.Trim();
                Client.TEL = Tel.Text.Trim();
                if (BllClient.Add(Client) < 0)
                {
                    ShowMessage("添加失败");
                }
                else
                {
                    Response.Redirect("ClientSearch.aspx");
                }
            }
            else
            {
                ShowMessage("该客户名已被使用，请输入其他用户名");
            }
        }
        else
        {
            string ID = Request.QueryString["ID"].ToString();
            T_CLIENT Client = BllClient.GetClientByID(ID);
            Client.ADDRESS = Address.Text.Trim();
            Client.CONTACT = Contact.Text.Trim();
            Client.NAME = Name.Text.Trim();
            Client.TEL = Tel.Text.Trim();
            if (BllClient.Update(Client) < 0)
            {
                ShowMessage("修改失败");
            }
            else
            {
                Response.Redirect("ClientSearch.aspx");
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("ClientSearch.aspx");
    }
}
